package com.mobile.collect.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.mobile.collect.R;
import com.mobile.collect.beans.DescribeInfo;
import com.mobile.collect.utils.FileUtil;
import com.mobile.collect.utils.XMLUtil;

import java.util.List;

import cn.droidlover.xdroidmvp.base.BaseRecyclerAdapter;
import cn.droidlover.xdroidmvp.base.ListViewItemClickListener;
import cn.droidlover.xdroidmvp.base.RecyclerViewHolder;

/**
 * 类名: KeyBroadPop
 * 作者: 李学伟
 * 日期: 2020年09月22日 16:39
 * 描述: dialog_keybroad
 */
public class DescDialog extends InputDialog {

    RecyclerView numberView;
    BaseRecyclerAdapter<DescribeInfo> listAdapter;
    String mDesc = "";

    public DescDialog(@NonNull Context context, String label, String oldTxt, InputCompleteListener listener) {
        super(context, label, oldTxt, listener, R.layout.dialog_number, 0);
    }

    public DescDialog(@NonNull Context context, String label, InputCompleteListener listener) {
        super(context, label, listener, R.layout.dialog_number, 0);
    }

    @Override
    protected void initViews() {
        super.initViews();
//        leftBtn.setVisibility(View.GONE);
//        rightBtn.setVisibility(View.GONE);
        inputEt.setVisibility(View.GONE);
        numberView = findViewById(R.id.numberView);
        numberView.setLayoutManager(new LinearLayoutManager(context));
        listAdapter = new BaseRecyclerAdapter<DescribeInfo>(context) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.item_latters;
            }

            @Override
            public void bindData(RecyclerViewHolder holder, int position) {
                super.bindData(holder, position);
                holder.setText(R.id.valueTv, getItemByPosition(position).getName());
            }
        };
        numberView.setAdapter(listAdapter);
        listAdapter.addList(true, getNumbers());
        listAdapter.setItemClickListener(new ListViewItemClickListener() {
            @Override
            public void itemClick(int position) {
                DescribeInfo describeInfo = listAdapter.getItemByPosition(position);
                mDesc = mDesc + describeInfo.getName();
                List<DescribeInfo> childList = describeInfo.getChildList();
                if (null != childList && childList.size() > 0) {
                    listAdapter.addList(true, childList);
                } else {
                    completeListener.onInputComplete(mDesc);
                    dismiss();
                }
            }
        });
        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                completeListener.onInputComplete(mDesc);
                dismiss();
            }
        });
        leftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    private List<DescribeInfo> getNumbers() {
        XMLUtil xmlUtil = new XMLUtil(FileUtil.getFromAssets(getContext(), "data.xml"));
        List<DescribeInfo> datas = xmlUtil.parserDescType();
        return datas;
    }


}
